package com.jieshuibao.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.config.Global;
import com.jieshuibao.common.res.BaseRes;
import com.jieshuibao.model.db.Policy;
import com.jieshuibao.model.db.PolicyExample;
import com.jieshuibao.model.db.PolicyQa;
import com.jieshuibao.model.db.PolicyQaExample;
import com.jieshuibao.model.db.PolicyRelated;
import com.jieshuibao.model.db.PolicyRelatedExample;
import com.jieshuibao.model.db.PolicyExample.Criteria;
import com.jieshuibao.model.domain.PolicyDetails;
import com.jieshuibao.model.domain.PolicyDetailsRes;
import com.jieshuibao.model.domain.PolicyListRes;
import com.jieshuibao.model.domain.PolicyReq;
import com.jieshuibao.service.PolicyQaService;
import com.jieshuibao.service.PolicyRelatedService;
import com.jieshuibao.service.PolicyService;
import com.jieshuibao.util.DeleteHtmlUtil;

@Controller
@RequestMapping(value="/policy")
public class PolicyController {
	
	@Resource
	private PolicyService policyService;
	
	@Resource
	private PolicyRelatedService policyRelatedService;
	
	@Resource
	private PolicyQaService policyQaService;
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public  Object PolicyList(HttpServletRequest request,
			@RequestBody PolicyReq req) {
			PolicyListRes res = new PolicyListRes();
			
		try {
			int sinceId = req.getSinceId();
			int maxId = req.getMaxId();
			
			PolicyExample policyExample = new PolicyExample();
			policyExample.setOrderByClause("policy.createDate desc");
			policyExample.setPageIndex(sinceId-1);
			policyExample.setPageSize(maxId - sinceId + 1);
			Criteria createCriteria = policyExample.createCriteria();
			createCriteria.andDelFlagEqualTo(Global.SHOW);

			List<Policy> data = policyService.getPagePolicy(policyExample);
			
			
			for (Policy policy : data) {
				String content = policy.getContent();
				
		
				if(!StringUtils.isEmpty(content)){
					String textFromHtml = DeleteHtmlUtil.getTextFromHtml(content);
					policy.setContent(textFromHtml);
				}
				policy.setExpert(null);
				policy.setOfficial(null);
				
			}
			res.setData(data);
			res.setSinceId(sinceId);
			res.setMaxId(sinceId-1+data.size());
			res.setRows(data.size());
			res.setCode(BaseRes.SUCCESS_CODE);	
			res.setDes(BaseRes.SUCCESS_DES);
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			
			res.setCode(BaseRes.FAIL_CODE);
			res.setDes("网络抖动了");
			return res;
			
		}

	}
	
	@RequestMapping(value = "/details", method = RequestMethod.POST)
	@ResponseBody
	public  Object policyDetails(HttpServletRequest request,
			@RequestBody PolicyReq req) {
		PolicyDetailsRes res=new PolicyDetailsRes();
		try {
		
			String uuid = req.getUuid();
			String type = req.getType();
			if(StringUtils.isEmpty(uuid)){
				res.setCode(BaseRes.ERRORPARAM_CODE);	
				res.setDes("uuid为空");
				return res;
			}
			
			if(StringUtils.isEmpty(type)){
				res.setCode(BaseRes.ERRORPARAM_CODE);	
				res.setDes("Type类型为空");
				return res;
			}
			Policy policyByUuid = policyService.getPolicyByUuid(uuid);
			
			if(policyByUuid==null){
				res.setCode(BaseRes.ERRORPARAM_CODE);	
				res.setDes("uuid错误");
				return res;
			}
			PolicyDetails policyDetails = new PolicyDetails();
			if(type.equals("policy")){
				policyDetails.setContent(policyByUuid.getContent());
				policyDetails.setTitle(policyByUuid.getTitle());
				policyDetails.setCreateDate(policyByUuid.getCreateDate());
			}else{
				policyDetails.setExpert(policyByUuid.getExpert());
				policyDetails.setOfficial(policyByUuid.getOfficial());
				PolicyRelatedExample policyRelatedExample = new PolicyRelatedExample();
				policyRelatedExample.setOrderByClause("policy_related.createDate desc");
				com.jieshuibao.model.db.PolicyRelatedExample.Criteria createCriteria = policyRelatedExample.createCriteria();
				createCriteria.andPolicyIdEqualTo(uuid);
				createCriteria.andDelFlagEqualTo(Global.SHOW);
				List<PolicyRelated> policyRelateds = policyRelatedService.getPolicyRelateds(policyRelatedExample);
				for (PolicyRelated policyRelated : policyRelateds) {
					String content = policyRelated.getContent();
					if(!StringUtils.isEmpty(content)){
						String textFromHtml = DeleteHtmlUtil.getTextFromHtml(content);
						
						policyRelated.setContent(textFromHtml);
						
					}
				}
				
				policyDetails.setPolicyRelateds(policyRelateds);
				PolicyQaExample policyQaExample = new PolicyQaExample();
				policyQaExample.setOrderByClause("policy_qa.createDate desc");
				com.jieshuibao.model.db.PolicyQaExample.Criteria createCriteria2 = policyQaExample.createCriteria();
				createCriteria2.andPolicyIdEqualTo(uuid);
				createCriteria2.andDelFlagEqualTo(Global.SHOW);
				
				List<PolicyQa> policyQas = policyQaService.getPolicyQas(policyQaExample);
				
				
				for (PolicyQa policyQa : policyQas) {
					String content = policyQa.getContent();
					
					if(!StringUtils.isEmpty(content)){
						String textFromHtml = DeleteHtmlUtil.getTextFromHtml(content);
						policyQa.setContent(textFromHtml);
					}
					
				}
				policyDetails.setPolicyQas(policyQas);
			}
				
			res.setData(policyDetails);
			res.setCode(BaseRes.SUCCESS_CODE);	
			res.setDes(BaseRes.SUCCESS_DES);
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			
			res.setCode(BaseRes.FAIL_CODE);
			res.setDes("网络抖动了");
			return res;
			
		}

	}
	@RequestMapping(value = "/relate/details", method = RequestMethod.POST)
	@ResponseBody
	public  Object relateDetails(HttpServletRequest request,
			@RequestBody PolicyReq req) {
		BaseRes<PolicyRelated> res = new BaseRes<PolicyRelated>();
		
			
		try {
		
			String uuid = req.getUuid();
			if(StringUtils.isEmpty(uuid)){
				res.setCode(BaseRes.ERRORPARAM_CODE);	
				res.setDes("uuid为空");
				return res;
			}
			PolicyRelated data = policyRelatedService.getPolicyRelatedByUuid(uuid);
			
			if(data==null){
				res.setCode(BaseRes.ERRORPARAM_CODE);	
				res.setDes("uuid错误");
				return res;
			}
			res.setData(data);
			res.setCode(BaseRes.SUCCESS_CODE);	
			res.setDes(BaseRes.SUCCESS_DES);
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			
			res.setCode(BaseRes.FAIL_CODE);
			res.setDes("网络抖动了");
			return res;
			
		}

	}
	
	@RequestMapping(value = "/qa/details", method = RequestMethod.POST)
	@ResponseBody
	public  Object qaDetails(HttpServletRequest request,
			@RequestBody PolicyReq req) {
		BaseRes<PolicyQa> res = new BaseRes<PolicyQa>();;
			
		try {
			String uuid = req.getUuid();
			if(StringUtils.isEmpty(uuid)){
				res.setCode(BaseRes.ERRORPARAM_CODE);	
				res.setDes("uuid为空");
				return res;
			}
			PolicyQa data = policyQaService.getPolicyQaByUuid(uuid);
			if(data==null){
				res.setCode(BaseRes.ERRORPARAM_CODE);	
				res.setDes("uuid错误");
				return res;
			}
			res.setData(data);
			res.setCode(BaseRes.SUCCESS_CODE);	
			res.setDes(BaseRes.SUCCESS_DES);
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			
			res.setCode(BaseRes.FAIL_CODE);
			res.setDes("网络抖动了");
			return res;
			
		}

	}
	

}
